package pro.cherish.smartfile.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import pro.cherish.smartfile.config.mybatis.handler.MybatisObjectHandler;

import javax.sql.DataSource;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-5-27 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：MybatisConfig
 * @Description 功能说明：
 * <p>

 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-5-27 
 */
@Slf4j
@Configuration
@MapperScan({"pro.cherish.smartfile.mapper"})
public class MybatisConfig {


	/**
	 * @Description 分页插件配置
	 * @param
	 * @Return com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
	 * @Author lys
	 * @Date 2020-5-27 9:19
	 **/
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
		return paginationInterceptor;
	}


	/**
	 * @Description 公用字段配置
	 * @param
	 * @Return mybatis.handler.MybatisObjectHandler
	 * @Author lys
	 * @Date 2020-5-27 9:19
	 **/
	@Bean
	@ConditionalOnMissingBean(MybatisObjectHandler.class)
	public MybatisObjectHandler MybatisObjectHandler() {
		return new MybatisObjectHandler();
	}

	/**
	 * @Description 数据源配置
	 * @param
	 * @Return com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder
	 * @Author lys
	 * @Date 2020-5-27 9:19
	 **/
	@Bean(name = "dataSource")
	@ConfigurationProperties("spring.datasource.druid")
	public DataSource dataSource() {
		return DruidDataSourceBuilder.create().build();
	}


	/**
	 * @Description 事务管理器配置
	 * @param dataSource
	 * @Return org.springframework.jdbc.datasource.DataSourceTransactionManager
	 * @Author lys
	 * @Date 2020-5-27 9:20
	 **/
	@DependsOn("dataSource")
	@Bean(name = "transactionManager")
	public DataSourceTransactionManager transactionManager(
			@Qualifier(value = "dataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
}
